<?php
define('FILE_ROOT', dirname(__FILE__).'/..');
set_include_path('.:'.FILE_ROOT.'/private/');

require_once 'Signals.php';
require_once 'Functions.php';
require_once 'Controller.php';
require_once 'Model.php';
require_once 'View.php';

spl_autoload_register('generic_autoload');
register_shutdown_function('ModelConfig::end');

ModelConfig::set('cookie', tempnam('/tmp', '1AND1_API_'));
$fo = fopen("php://stdin", "r");

do {
	do {
		echo View::showPrompt();
		$q = fgets($fo);
		$q = ($q === false) ? 'exit' : trim($q);
	} while (empty($q));

	ModelHistory::add($q);
	try {
		$a = Controller::newInstance($q)->execute();
	} catch (SignalArgUnknown $e) {
			View::show("[-] Unrecognized argument: {$e->getMessage()}");
	} catch (SignalCmdUnknown $e) {
		View::show("[-] Command not found: $q");
	} catch (SignalExit $e) {
		break;
	} catch (Exception $e) {
		View::show("[!] Caught an exception\n".$e->getTraceAsString());
	}
} while(true);